package com.geostar.constructionland.analyse.service.geohandler;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.geohandler.ErasureLandModel;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.service.AnalyseService;
import com.geostar.constructionland.common.global.ConstAnalyseType;
import com.geostar.constructionland.common.service.logs.JsydFxrzService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

@Service
public class GeoHandlerService {

    @Resource
    private AnalyseService analyseService;

    @Resource
    private ObjectMapper objectMapper;

    @Resource
    private JsydFxrzService jsydFxrzService;

    /**
     * 擦除地块
     *
     * @param erasureLandModel
     */
    public Map<String, Object> erasureLand(ErasureLandModel erasureLandModel) throws Exception {
        /** 根据ywid查找地块GEOJSON JSYD_DKXX**/
        AnalyseModel model = new AnalyseModel();
        model.setFxlx(ConstAnalyseType.CONST_KJCC);
        model.setYwid(erasureLandModel.getYwid());
        model.setOriginFxlx(erasureLandModel.getFxlx());
        model.setDlIds(erasureLandModel.getDlIds());
        HandlerResult result = analyseService.analyse(model);
        Map<String, Object> resultMap = objectMapper.readValue(result.getData().toString(), Map.class);


        /** 根据分析表示以及fxlx、dlid,fxbs 查找擦除地块的信息以及MongoDB中的geojson**/
        /** 构建空间擦除参数**/
        /**调用吉威空间擦除服务**/
        /**调用吉威空间落图服务**/
//        byte[] bytes = null;
        Map<String, Object> result2Map = null;
        if (resultMap.get("StatusCode") != null && "200".equals(resultMap.get("StatusCode").toString())) {
            String uniquetaskid = resultMap.get("UniqueTaskID").toString();
            if (StringUtils.isNotBlank(uniquetaskid)) {
                AnalyseModel model2 = new AnalyseModel();
                model2.setFxlx(ConstAnalyseType.CONST_YZFXLT);
                model2.setYwid(erasureLandModel.getYwid());
                model2.setOriginFxlx(erasureLandModel.getFxlx());
                model2.setUniqueTaskId(uniquetaskid);
                HandlerResult result2 = analyseService.analyse(model2);
                result2Map = objectMapper.readValue(result2.getData().toString(), Map.class);

            }
        }


        return result2Map;
    }

    /**
     * @Date 2020-11-23 04:02:37
     * @Description
     **/
    public Map<String, Object> exportMdb(String ywid, String fxlx) throws Exception {
        String uniqueTaskId = jsydFxrzService.getFxrzByYwidAndFxlx(ywid, fxlx).getUniqueTaskId();

        AnalyseModel model = new AnalyseModel();
        model.setFxlx(ConstAnalyseType.CONST_YZFXLT);
        model.setYwid(ywid);
        model.setOriginFxlx(fxlx);
        model.setUniqueTaskId(uniqueTaskId);
        HandlerResult result = analyseService.analyse(model);
        Map<String, Object> resultMap = objectMapper.readValue(result.getData().toString(), Map.class);
        return resultMap;
    }


}
